package lee.config;

import lee.filter.AuthFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.security.config.annotation.web.configurers.UrlAuthorizationConfigurer;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import javax.servlet.FilterRegistration;
import java.util.ArrayList;
import java.util.List;

/**
 * @Autor zhangjiawen
 * @Date: 2020/5/29 9:56
 */
@Configuration
public class ZuulConfig {

    @Bean
    public AuthFilter preAuthFilter(){
        return new AuthFilter();
    }

    @Bean
    public FilterRegistrationBean corsFilter(){
        final UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config=new CorsConfiguration();
        config.setAllowCredentials(true);
        List<String> ruleList=new ArrayList<>();
        ruleList.add("*");
        config.setAllowedOrigins(ruleList);
        config.setAllowedHeaders(ruleList);
        config.setAllowedMethods(ruleList);
        config.setMaxAge(1800L);
        source.registerCorsConfiguration("/**",config);
        CorsFilter corsFilter=new CorsFilter(source);
        FilterRegistrationBean bean=new FilterRegistrationBean(corsFilter);
        bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return bean;

    }
}
